﻿using System;
using System.Linq;
using System.Web.Security;
using GalaxyTranslator.EntityDataModel;
using GalaxyTranslator.Model;

namespace GalaxyTranslator.MyMembership
{
	public class MyMembershipProvider : MembershipProvider
	{
		public override string ApplicationName
		{
			get { return null; }
			set { /* not implemented*/ }
		}

		public override bool ChangePassword(string username, string oldPassword, string newPassword)
		{
			// not implemented
			return false;
		}

		public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
		{
			// not implemented
			return false;
		}

		public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
		{
			using (var context = new GalaxyTranslatorEntities())
			{
				if (string.IsNullOrEmpty(username))
				{
					status = MembershipCreateStatus.InvalidUserName;
					return null;
				}
				if (string.IsNullOrEmpty(password))
				{
					status = MembershipCreateStatus.InvalidPassword;
					return null;
				}
				if (context.MyUsers.Any(u => u.Login == username))
				{
					status = MembershipCreateStatus.DuplicateUserName;
					return null;
				}

				var newUser = new MyUser {Login = username, Password = Sha1Helper.GetSha1Hash(password)};
				context.MyUsers.Add(newUser);
				context.SaveChanges();
				status = MembershipCreateStatus.Success;
				return new MyMembershipUser(Membership.Provider.Name, username, newUser.Id);
			}
		}

		public override bool DeleteUser(string username, bool deleteAllRelatedData)
		{
			using (var context = new GalaxyTranslatorEntities())
			{
				if (string.IsNullOrWhiteSpace(username))
					return false;

				MyUser user = DataLayer.GetUserByName(username);
				if (user == null)
					return false;

				context.MyUsers.Remove(user);
				context.SaveChanges();
				return true;
			}
		}

		public override bool EnablePasswordReset
		{
			get { return false; }
		}

		public override bool EnablePasswordRetrieval
		{
			get { return false; }
		}

		public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
		{
			// not implemented
			totalRecords = 0;
			return null;
		}

		public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
		{
			// not implemented
			totalRecords = 0;
			return null;
		}

		public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
		{
			// not implemented
			totalRecords = 0;
			return null;
		}

		public override int GetNumberOfUsersOnline()
		{
			// not implemented
			return 0;
		}

		public override string GetPassword(string username, string answer)
		{
			// not implemented
			return null;
		}

		public override MembershipUser GetUser(string username, bool userIsOnline)
		{
			// not implemented
			return null;
		}

		public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
		{
			// not implemented
			return null;
		}

		public override string GetUserNameByEmail(string email)
		{
			// not implemented
			return null;
		}

		public override int MaxInvalidPasswordAttempts
		{
			get { return 42; }
		}

		public override int MinRequiredNonAlphanumericCharacters
		{
			get { return 0; }
		}

		public override int MinRequiredPasswordLength
		{
			get { return 4; }
		}

		public override int PasswordAttemptWindow
		{
			get { return 0; }
		}

		public override MembershipPasswordFormat PasswordFormat
		{
			get { return MembershipPasswordFormat.Hashed; }
		}

		public override string PasswordStrengthRegularExpression
		{
			get { return string.Empty; }
		}

		public override bool RequiresQuestionAndAnswer
		{
			get { return false; }
		}

		public override bool RequiresUniqueEmail
		{
			get { return false; }
		}

		public override string ResetPassword(string username, string answer)
		{
			// not implemented
			return null;
		}

		public override bool UnlockUser(string userName)
		{
			// not implemented
			return false;
		}

		public override void UpdateUser(MembershipUser user)
		{
			// not implemented
		}

		public override bool ValidateUser(string username, string password)
		{
			MyUser user = DataLayer.GetUserByName(username);
			if (user == null)
				return false;
			string hashedPass = user.Password;
			return Sha1Helper.VerifySha1Hash(password, hashedPass);
		}
	}
}